 
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JPanel;
import javax.swing.border.SoftBevelBorder;
 
/**
 *
 * @author laughing
 * 
 * @date 2014年11月16日 下午7:58:11
 */
@SuppressWarnings("serial")
public class GamePanel extends JPanel implements Runnable, KeyListener {
 
    private static final int    HEIGHT  = 480;
    private static final int    WEIGHT  = 320;
    Image[]                     plans   = new Image[6];
    int                         pos     = 0;
    int                         x       = WEIGHT / 2;
    int                         y       = 408;
    final int                   step    = 10;
 
    public GamePanel() {
        setSize(WEIGHT, HEIGHT);
        setBorder(new SoftBevelBorder(1, Color.white, Color.white));
        // Sets the focusable state of this Component to the specified value.
        // This value overrides the Component's default focusability.
        setFocusable(true);
        // 监听事件
        addKeyListener(this);
        new Thread(this).start();
    }
 
    /*
     * (non-Javadoc)
     * 
     * 
     * 
     * @see javax.swing.JComponent#paintComponent(java.awt.Graphics)
     */
    @Override
    protected void paintComponent(Graphics g0) {
        // TODO Auto-generated method stub
        super.paintComponent(g0);
        Graphics2D g = (Graphics2D) g0;
 
        try {
            for (int i = 0; i < 6; i++) {
                plans[i] = ImageIO.read(this.getClass()
                                            .getResource("../../images/plan_" + i + ".png"));
 
            }
            g.drawImage(plans[pos], x, y, this);
 
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
 
    }
 
    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Runnable#run()
     */
    @Override
    public void run() {
        // TODO Auto-generated method stub
        while (true) {
 
            try {
                pos++;
                if (pos == 5)
                    pos = 0;
                Thread.sleep(100);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            repaint();
        }
    }
 
    /*
     * (non-Javadoc)
     * 
     * @see java.awt.event.KeyListener#keyTyped(java.awt.event.KeyEvent)
     */
    @Override
    public void keyTyped(KeyEvent e) {
        // TODO Auto-generated method stub
 
    }
 
    /*
     * (non-Javadoc)
     * 
     * @see java.awt.event.KeyListener#keyPressed(java.awt.event.KeyEvent)
     */
    @Override
    public void keyPressed(KeyEvent e) {
 
        int key = e.getKeyCode();
        if (key == KeyEvent.VK_UP) {
            if (y - step >= 0) {
                y -= step;
            }
        }
        if (key == KeyEvent.VK_DOWN) {
 
            if (y + step <= HEIGHT - plans[0].getHeight(null) * 2) {
                y += step;
            }
        }
        if (key == KeyEvent.VK_LEFT) {
            if (x - step >= 0)
                x -= step;
        }
        if (key == KeyEvent.VK_RIGHT) {
 
            if (x + step < WEIGHT - plans[0].getWidth(null))
                x += step;
        }
 
    }
 
    /*
     * (non-Javadoc)
     * 
     * @see java.awt.event.KeyListener#keyReleased(java.awt.event.KeyEvent)
     */
    @Override
    public void keyReleased(KeyEvent e) {
        // TODO Auto-generated method stub
 
    }
}